﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
namespace FTchina.Api
{
    /// <summary>
    ///navbar 的摘要说明
    /// </summary>
    public class navbar
    {

        /// <summary>
        /// 获取导航条
        /// </summary>
        /// <param name="cid">栏目id</param>
        /// <returns></returns>
        public static string NavBar(string cid)
        {
            return NavBar(cid, "");
        }
        /// <summary>
        /// 获取导航条
        /// </summary>
        /// <param name="cid">栏目id</param>
        /// <param name="cid">栏目地址前缀</param>
        /// <returns></returns>
        public static string NavBar(string cid, string url)
        {
            if (FTchina.Caching.FTchinaCache.IsExits("Nvbar" + url + cid))
            {
                return FTchina.Caching.FTchinaCache.GetCache("Nvbar" + url + cid);
            }
            else
            {
                string sql = "select * from lanmu where parentid=" + state.RootColuID + " order by paixu desc,id desc";
                DataTable dt = shujuku.GetDataTable(sql);
                string tempwords = "<div id=\"navbar\"><div class=\"m\">";
                tempwords += basic.GetUserMenu("before");
                string hrefflag = string.Empty;//连接地址放这
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                   
                    //有外部连接则跳转
                    if (System.String.IsNullOrEmpty(dt.Rows[i]["link"].ToString()))
                    {
                        hrefflag = "\"" + basic.GetSiteMainWorkUrl() + url + "list" + dt.Rows[i]["id"].ToString() + ".aspx\" ";
                    }
                    else
                    {
                        hrefflag = "\"" + dt.Rows[i]["link"].ToString() + "\" target='_blank'";
                    }

                    string subnav = "onmouseover='menu.show(this," + i.ToString() + ",1,false)'";
                    if (dt.Rows[i]["id"].ToString() == cid)
                    {
                        tempwords += "<a href=\"" + hrefflag + subnav + "  class=\"thisclass\">" + dt.Rows[i]["context"].ToString() + "</a> ";
                    }
                    else
                    {
                        tempwords += "<a href= " +hrefflag+ subnav + ">" + dt.Rows[i]["context"].ToString() + "</a> ";
                    }
                }
                tempwords += basic.GetUserMenu("after");

                tempwords += "</div></div>";
                FTchina.Caching.FTchinaCache.AddCache("Nvbar" + url + cid, tempwords);
                return tempwords;
            }
        }

        /// <summary>
        /// 获取二级菜单
        /// </summary>
        /// <returns></returns>
        public static string GetMenuChild()
        {
            if (FTchina.Caching.FTchinaCache.IsExits("meunchild"))
            {
                return FTchina.Caching.FTchinaCache.GetCache("meunchild");
            }
            else
            {
                string meunchild = string.Empty;
                string sql = "select * from lanmu where parentid=" + state.RootColuID + " order by paixu desc,id desc";
                DataTable dt = shujuku.GetDataTable(sql,"lanmuinfo00");
                string hrefflag = string.Empty;//连接地址放这
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DataTable dts = shujuku.GetDataTable("select * from lanmu where ParentID=" + dt.Rows[i]["id"] + " order by paixu desc,id desc", "lanmuinfo" + dt.Rows[i]["id"]);
                    if (dts.Rows.Count > 0)
                    {
                       

                        meunchild += "linkset[" + i.ToString() + "]=[";
                        for (int g = 0; g < dts.Rows.Count; g++)
                        {
                            //有外部连接则跳转
                            if (System.String.IsNullOrEmpty(dts.Rows[g]["link"].ToString()))
                            {
                                hrefflag = basic.GetSiteMainWorkUrl() + "list.aspx?cid=" + dts.Rows[g]["id"];
                                
                            }
                            else
                            {
                                hrefflag = dts.Rows[g]["link"].ToString() ;
                            }

                            meunchild += "[\"" + dts.Rows[g]["context"] + "\",\"" + hrefflag + "\",false],";
                        }
                        meunchild = meunchild.Substring(0, meunchild.Length - 1);
                        meunchild += "];";
                    }
                    else
                    {
                        meunchild += "linkset[" + i.ToString() + "]=[];";
                    }
                }
                meunchild += "menu.done=true;";
                FTchina.Caching.FTchinaCache.AddCache("meunchild", meunchild);

                return meunchild;
            }
        }

        /// <summary>
        /// 通过栏目id获取栏目名称
        /// </summary>
        /// <param name="LanmuId"></param>
        /// <returns></returns>
        public static string GetLanmuNameByLanmuId(string LanmuId)
        {
            string tempwords = string.Empty;
            tempwords = shujuku.GetValueByKey("context,id", "lanmu", "id='" + LanmuId + "'", "Lname" + LanmuId);
            return tempwords;
        }












        #region 获取列表的分类目录
        public static string Templist = string.Empty;
        /// <summary>
        /// 获取列表的分类目录
        /// </summary>
        /// <param name="cid"></param>
        /// <returns></returns>
        public static string NavBarList(string cid)
        {
            return NavBarList(cid, "");
        }

        /// <summary>
        /// 获取取列表装的分类目录，可自定义列表URL前缀
        /// </summary>
        /// <param name="cid"></param>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string NavBarList(string cid, string url)
        {
            if (FTchina.Caching.FTchinaCache.IsExits("NavBarList" + cid + url))
            {
                return FTchina.Caching.FTchinaCache.GetCache("NavBarList" + cid + url);
            }
            else
            {
                Templist = "<ul class=\"sList\">";
                MakeTr("1", 0, cid, url); //开始执行迭代
                Templist += "</ul>";
                FTchina.Caching.FTchinaCache.AddCache("NavBarList" + cid + url, Templist);
                return Templist;
            }
        }

        /// <summary>
        /// 获取父菜单的id
        /// </summary>
        /// <param name="cid"></param>
        /// <returns></returns>
        private static string GetParentMenuid(string cid)
        {
            return shujuku.GetValueByKey("parentid", "lanmu", "id=" + cid,"parentid"+cid);
        }



        /// <summary>
        /// 检查是否有可用父菜单
        /// </summary>
        /// <param name="cid"></param>
        /// <returns></returns>
        private static Boolean ChkMenuParent(string cid)
        {
            return shujuku.GetValueByKey("parentid", "lanmu", "id=" + cid, "parentid" + cid) != "0";
        }
        /// <summary>
        /// 检查是否有可用子菜单
        /// </summary>
        /// <param name="cid"></param>
        /// <returns></returns>
        private static Boolean ChkMenuChild(string cid)
        {
            return shujuku.testread("select * from lanmu where parentid=" + cid);
        }



        /// <summary>
        /// 装配每一行
        /// </summary>
        /// <param name="id">上级ID</param>
        /// <param name="count">迭代次数</param>
        private static void MakeTr(string id, int count, string cid, string url)
        {
            string hrefflag = string.Empty;
            DataView dv = new DataView(shujuku.GetDataTable("select * from lanmu where parentid=" + id + " order by paixu desc,id desc","diedai"+id+cid+url));
            foreach (DataRowView drv in dv)
            {
                //有外部连接则跳转
                if (System.String.IsNullOrEmpty(drv["link"].ToString()))
                {
                    hrefflag = "\"" + basic.GetSiteMainWorkUrl() + url + "list" + drv["id"].ToString() + ".aspx\" ";
                }
                else
                {
                    hrefflag = "\"" + drv["link"].ToString() + "\" target='_blank'";
                }

                if (drv["id"].ToString() == cid)
                {
                    Templist += "<li>" + MakeFengefu(count) + "<a href="+hrefflag+"  class=\"sOpen\">" + drv["context"] + "(" + state.GetCountContent(drv["id"].ToString()) + ")</a></li> ";
                }
                else
                {
                    Templist += "<li>" + MakeFengefu(count) + "<a href= " + hrefflag + " class=\"sClose\" >" + drv["context"] + "(" + state.GetCountContent(drv["id"].ToString()) + ")</a></li> ";
                }
                //再次遍历
                MakeTr(drv["id"].ToString(), count + 1, cid, url);

            }

        }
        /// <summary>
        /// Make分隔符
        /// </summary>
        /// <param name="count"></param>
        /// <returns></returns>
        private static string MakeFengefu(int count)
        {
            string Returnwords = string.Empty;
            if (count == 0)
            {
                Returnwords = "";
            }

            else
            {
                Returnwords = ("　").PadLeft(count, '　');
            }
            return Returnwords;
        }

        #endregion


        #region 当前位置导航
        /// <summary>
        /// 导航指定的页面
        /// </summary>
        /// <param name="pagename">页面名称</param>
        /// <param name="url">lizi.aspx</param>
        /// <returns></returns>
        public static string NavSub(string pagename, string url)
        {
            return "<div id=\"navsub\"><div class=\"m\"><div style=\"float: left;\">  &nbsp;--->> " + pagename + "</div></div></div>";
        }
        /// <summary>
        /// 只输出占位符号
        /// </summary>
        /// <param name="pagename"></param>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string NavSub()
        {
            return "<div id=\"navsub\"><div class=\"m\"><div style=\"float: left;\">  &nbsp; </div></div></div>";
        }

        private static string artlj = string.Empty;

        /// <summary>
        /// 当前位置导航
        /// </summary>
        /// <param name="cid">栏目id</param>
        /// <returns></returns>
        public static string NavSub(string cid)
        {
            if (FTchina.Caching.FTchinaCache.IsExits("Nvsub" + cid))
            {
                return FTchina.Caching.FTchinaCache.GetCache("Nvsub" + cid);
            }
            else
            {
                artlj = "";
                string tempwords = "<div id=\"navsub\"><div class=\"m\"><div style=\"float: left;\">";
                tempwords += "  &nbsp; <a href=\"" + basic.GetSiteMainWorkUrl()+  "default.aspx\">首页</a> " + MLcc(cid) + shujuku.GetValueByKey("context", "lanmu", "id=" + cid) + "</a>";
                tempwords += "</div></div></div>";
                FTchina.Caching.FTchinaCache.AddCache("Nvsub" + cid, tempwords);
                return tempwords;
            }
        }
        private static string LinkType(string id)
        {
            string words = null;
            string rlins = shujuku.GetValueByKey("link,id", "lanmu", "id=" + id, "reallinks");
            words = (rlins == "") ? "<a href='" + basic.GetSiteMainWorkUrl() + "list" + id + ".aspx'>" : "<a href='" + rlins + "'>";
            return words;
        }
        //递归形成路径层次
        private static string MLcc(string lmid)
        {
            if (FTchina.Caching.FTchinaCache.IsExits("artlj" + lmid))
            {
                return FTchina.Caching.FTchinaCache.GetCache("artlj" + lmid);
            }
            else
            {
                string MLccSql = "select top 1 id,ParentID,ConText from lanmu where id=" + lmid + " and parentid<>0 ";
                if (shujuku.testread(MLccSql))
                {
                    DataTable dt = shujuku.GetDataTable(MLccSql);
                    string Parentcontext = shujuku.GetValueByKey("context", "lanmu", " parentid<>0 and id=" + dt.Rows[0]["ParentID"]);
                    if (dt.Rows[0]["ParentID"].ToString() != "0")
                    {
                        artlj = LinkType(dt.Rows[0]["ParentID"].ToString()) + Parentcontext + "</a>  →" + artlj;
                        MLcc(dt.Rows[0]["ParentID"].ToString());
                    }

                }
                FTchina.Caching.FTchinaCache.AddCache("artlj" + lmid, artlj);

                return artlj;
             }
           
        }
        #endregion
    }
}